package cn.iocoder.yudao.module.cdk.controller.app.cdkuser;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.ratelimiter.core.annotation.RateLimiter;
import cn.iocoder.yudao.module.cdk.controller.admin.cdk.vo.CdkRespVO;
import cn.iocoder.yudao.module.cdk.controller.app.cdkuser.vo.CdkappuserSaveReqVO;
import cn.iocoder.yudao.module.cdk.dal.dataobject.cdk.CdkDO;
import cn.iocoder.yudao.module.cdk.service.cdkcdkuser.CdkAPPCdkuserService;
import cn.iocoder.yudao.module.cdk.service.cdkuser.CdkappuserService;
import cn.licoy.encryptbody.annotation.decrypt.DESDecryptBody;
import cn.licoy.encryptbody.annotation.encrypt.DESEncryptBody;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - cdk使用用户")
@RestController
@RequestMapping("/cdk/user")
@Validated
@DESEncryptBody(key = "ashduiawbd@833Has")
public class AppCdkuserController {

    @Resource
    private CdkappuserService appuserService;

    @Resource
    private CdkAPPCdkuserService appcdkuserService;

    @Autowired
    private JsonUtils jsonUtils;

    @RateLimiter(count = 20, timeUnit = TimeUnit.MINUTES)
    @PostMapping("/create")
    @Operation(summary = "创建cdk使用用户")
    @DESDecryptBody
    public CommonResult<Integer> createuser(@RequestBody String createReqVO) {
        if (jsonUtils.isJson(createReqVO)) {
            String name = String.valueOf(JsonUtils.parseTree(createReqVO).get("time").textValue());

            CdkappuserSaveReqVO c = new CdkappuserSaveReqVO();
            c.setName(name);
            return success(appuserService.createuser(c));
        } else {
            return success(1);
        }
    }
//
//    @GetMapping("/get")
//    @Operation(summary = "获得cdk使用用户")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('cdk:user:query')")
//    public CommonResult<CdkappuserRespVO> getuser(@RequestParam("id") Integer id) {
//        CdkuserDO user = appcdkuserService.getuser(id);
//        return success(BeanUtils.toBean(user, CdkappuserRespVO.class));
//    }


    @Operation(summary = "查询cdk")
    @PostMapping("/find")
    @DESDecryptBody
    public CommonResult<CdkRespVO> find(@RequestBody String time) {
        if (jsonUtils.isJson(time)) {

            CdkDO user = appcdkuserService.find(time);
            return success(BeanUtils.toBean(user, CdkRespVO.class));
        } else {
            return success(BeanUtils.toBean(new CdkRespVO(), CdkRespVO.class));
        }
    }


}